import 'package:flutter/material.dart';
import 'package:sainbainu/common/values/colors.dart';

class PageWrapper extends StatelessWidget {
  /// 加载页
  final bool showLoadingPage;
  final bool loadingProgress;
  final Color? loadingProgressBgColor;
  final Animation<Color?>? loadingProgressValueColor;
  final String? loadingText;
  final TextStyle? loadingTextStyle;
  final Widget? loadingTextChild;

  /// 结果页
  final bool showResultPage;

  /// 结果页描述
  final String? resultDesc;
  final TextStyle? resultDescStyle;
  final Widget? resultDescChild;

  /// 结果页按钮
  final bool resultBtn;
  final String? resultBtnText;
  final ButtonStyle? resultBtnStyle;
  final Widget? resultBtnChild;
  final VoidCallback? onResultBtnTap;

  /// 正文
  final Widget child;

  const PageWrapper({
    super.key,
    this.showLoadingPage = true,
    this.loadingProgress = true,
    this.loadingProgressBgColor,
    this.loadingProgressValueColor,
    this.loadingText,
    this.loadingTextStyle,
    this.loadingTextChild,
    this.showResultPage = false,
    this.resultDesc,
    this.resultDescStyle,
    this.resultDescChild,
    this.resultBtn = false,
    this.resultBtnText,
    this.resultBtnStyle,
    this.resultBtnChild,
    this.onResultBtnTap,
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    if (showLoadingPage) {
      return _buildLoadingPage();
    } else if (showResultPage) {
      return _buildResultPage();
    } else {
      return child;
    }
  }

  /// 加载页
  Widget _buildLoadingPage() {
    List<Widget> list = [];
    if (loadingProgress) {
      list.add(CircularProgressIndicator(
        backgroundColor: loadingProgressBgColor,
        valueColor: loadingProgressValueColor ??
            const AlwaysStoppedAnimation(AppColors.active),
      ));
    }
    if (loadingText != null) {
      list.add(_buildLoadingText());
    }
    if (list.length == 2) {
      list.insert(1, const SizedBox(height: 10));
    }
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: list,
      ),
    );
  }

  /// 加载页文字
  Widget _buildLoadingText() {
    return loadingTextChild ??
        Text(
          loadingText!,
          style: loadingTextStyle ?? const TextStyle(fontSize: 15),
        );
  }

  /// 结果页
  Widget _buildResultPage() {
    List<Widget> list = [];
    if (resultDesc != null) {
      list.add(_buildResultDesc());
    }
    if (resultBtn) {
      list.add(_buildResultBtn());
    }
    if (list.length == 2) {
      list.insert(1, const SizedBox(height: 10));
    }
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: list,
      ),
    );
  }

  /// 结果页描述
  Widget _buildResultDesc() {
    return resultDescChild ??
        Text(
          resultDesc!,
          style: resultDescStyle ?? const TextStyle(fontSize: 15),
        );
  }

  /// 结果页按钮
  Widget _buildResultBtn() {
    return resultBtnChild ??
        ElevatedButton(
          onPressed: onResultBtnTap,
          style: resultBtnStyle ??
              ElevatedButton.styleFrom(
                backgroundColor: AppColors.danger,
                foregroundColor: Colors.white,
              ),
          child: Text(resultBtnText ?? ''),
        );
  }
}
